//
//  Bullet.m
//  Project5
//
//  Created by Nguyen Tan Loc on 5/20/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "Bullet.h"

@implementation Bullet
@synthesize _radius = m_Radius, _state = m_State, _color = m_Color, _type = m_Type;

-(id) initWithType:(int)t color:(int)c
{
    if (self = [super init])
    {
        m_Type = t;
        m_Color = t;
        m_Radius = 8.0f;
        [self createSprite];
    }
    
    return self;
}

-(void) createSprite
{
    switch (m_Type)
    {
        case BULLET_TYPE_NORMAL:
            switch (m_Color)
            {
                case SEGMENT_COLOR_BLUE:
                    m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_BLUE"];
                    break;
                    
                case SEGMENT_COLOR_GREEN:
                    m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_GREEN"];
                    break;
                    
                case SEGMENT_COLOR_RED:
                    m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_RED"];
                    break;
                    
                case SEGMENT_COLOR_YELLOW:
                    m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_YELLOW"];
                    break;
                    
                case SEGMENT_COLOR_PURPLE:
                    m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_PURPLE"];
                    break;
                    
                default:
                    break;
            }
            break;
            
        case BULLET_TYPE_POWER_1:
            m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_RED"];
            break;
            
        case BULLET_TYPE_POWER_2:
            m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_RED"];
            break;
            
        case BULLET_TYPE_POWER_FULL:
            m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_RED"];
            break;
            
        case BULLET_TYPE_UNSTOPABLE:
            m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_RED"];
            break;
            
        default:
            break;
    }
    
    
    float ratio = m_Radius / (m_Sprite.contentSize.width / 2);
    [m_Sprite setScale:ratio];
    [self addChild:m_Sprite];
}


-(void) setAngle:(float)a
{
    m_SpeedX = cosf(CC_DEGREES_TO_RADIANS(a)) * BULLET_SPEED;
    m_SpeedY = sinf(CC_DEGREES_TO_RADIANS(a)) * BULLET_SPEED;
}

-(void) appear
{
    [self setScale:0.0f];
    CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.2f scale:1.0f];
    CCEaseBackOut *bounceIn = [CCEaseBackOut actionWithAction:scaleTo];
    CCDelayTime *delay = [CCDelayTime actionWithDuration:0.05];
    CCSequence *seq = [CCSequence actionOne:delay two:bounceIn];
    [self runAction:seq];
}

-(void) fireWithAngle:(float)a
{
    [self setAngle:a];
    m_State = STATE_ACTIVE;
    [self scheduleUpdate];
}

-(void) randomColor
{
    m_Color = rand() % NUM_COLOR;
    [m_Sprite removeFromParentAndCleanup:YES];
    [self createSprite];
}

-(void) update:(ccTime)delta
{
    float newX = self.position.x + m_SpeedX * delta;
    float newY = self.position.y + m_SpeedY * delta;
    [self setPosition:ccp(newX, newY)];
    
    int constOut = 100;
    CGSize size = [[CCDirector sharedDirector] winSize];
    if ((newX < -constOut - (size.width / 2)) || (newX + m_Radius > (size.width / 2) + constOut)
        || (newY < -constOut - (size.height / 2)) || (newY + m_Radius > (size.height / 2) + constOut))
    {
        [self resetBullet];
    }
}

-(void) explode
{
    // add effecct here
    if (m_Type != BULLET_TYPE_UNSTOPABLE)
    {
        [self resetBullet];
    }
    else
    {
        
    }
}

-(void) resetBullet
{
    [self unscheduleUpdate];
    [self setPosition:ccp(0.0f, 0.0f)];
    [self setVisible:NO];
    m_State = STATE_IDLE;
    m_Type = BULLET_TYPE_NORMAL;
}

-(void) setType:(int)t
{
    
}

@end
